package com.fw.mediasession.media.mediasession.sessionutils;

import android.os.Bundle;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import com.fw.mediasession.media.bean.BaseBean;
import com.orhanobut.logger.Logger;

import java.util.Timer;
import java.util.TimerTask;

import static com.fw.mediasession.media.mediasession.MediaSessionConstants.CURRENT_INDEX;
import static com.fw.mediasession.media.mediasession.MediaSessionConstants.CURRENT_POSITION;


public class MediaSessionToClientManager implements MediaSessionToClient {
    private MediaSessionCompat mediaSessionCompat;
    private final PlaybackStateCompat.Builder builder;
String TAG="MediaSessionToClientManager: ";
    private Timer timer;
    private TimerTask timerTask;
    public MediaSessionToClientManager(MediaSessionCompat mediaSessionCompat) {
        this.mediaSessionCompat = mediaSessionCompat;
        builder = new PlaybackStateCompat.Builder();
        PlaybackStateCompat mPlaybackState = builder
                .setState(PlaybackStateCompat.STATE_NONE, 0, 1.0f)
                .build();
        mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaSessionCompat.setPlaybackState(mPlaybackState);
    }

    @Override
    public void sendMediaInfo(BaseBean baseBean) {
        MediaMetadataCompat mediaMetadataCompat = new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, baseBean.ID)
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, baseBean.album)
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, baseBean.artist)
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, baseBean.title)
                .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, baseBean.title)
                .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, baseBean.artist)
                .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, baseBean.duration)
                .putLong(MediaMetadataCompat.METADATA_KEY_DOWNLOAD_STATUS, baseBean.duration)
                .build();
//        .putString(Constants.TYPE_MEDIA_CONNECTOR, Constants.TYPE_MEDIA_CONNECTOR_USB1.toString())
//        .putLong(Constants.KEY_CURRENT_POSITION, mediaPlayer.currentPosition.toLong())

        mediaSessionCompat.setMetadata(mediaMetadataCompat);
    }


    /**
     *
     * @param state 播放状态
     * @param currentPosition 播放当前时间
     */
    @Override
    public void sendMediaState(int state,long currentPosition) {
//     mediaSessionCompat.setPlaybackState();
        PlaybackStateCompat build = builder.setState(state, currentPosition, 1.0f)
                .build();
        mediaSessionCompat.setPlaybackState(build);
        Logger.d(TAG+"state :"+state);

    }





    public static BaseBean getBaseBeanFromMediaMetadataCompat(MediaMetadataCompat mediaMetadataCompat) {
        BaseBean baseBean = new BaseBean();
        baseBean.ID = mediaMetadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
        baseBean.album = mediaMetadataCompat.getString(MediaMetadataCompat.METADATA_KEY_ALBUM);
        baseBean.artist = mediaMetadataCompat.getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
        baseBean.title = mediaMetadataCompat.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
        baseBean.duration = mediaMetadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_DURATION);
        return baseBean;
    }
    @Override
    public void updatePlayPosition( int currentPosition) {

        Bundle bundle=new Bundle();
        bundle.putInt(CURRENT_POSITION,currentPosition);
        mediaSessionCompat.setExtras(bundle);
//        Logger.d(TAG+"currentPosition :"+currentPosition);
    }

    @Override
    public void updateIndex(int index) {
        Bundle bundle=new Bundle();
        bundle.putInt(CURRENT_INDEX,index);
        mediaSessionCompat.setExtras(bundle);
    }


}
